﻿#include "MyMain.h"
#include "global.h"
#include "TankScore.h"
#include "SetStage.h"
#include "HitFx.h"
#include "Shield.h"
#include "Water.h"
#include "Bullet.h"
#include "SimpleAudioEngine.h"
#include "SysMenu.h"
#include "Dialog.h"
//#include "Jni1.h"
char a[20];
GameOver *MyMain::mygameover=NULL;
CCScene *MyMain::scene() {
	CCScene* s = CCScene::create();
	CCLayer* l = MyMain::create();
	s->addChild(l);
	return s;
}

bool MyMain::containp( CCRect r,  CCPoint p) {
	CCPoint p2 = r.origin;
	if (p.x > p2.x && p.x < p2.x + r.size.width && p.y > p2.y
		&& p.y < p2.y + r.size.height)
		return true;
	else
		return false;
}

void MyMain::directp( CCRect r,  CCPoint p) {
	CCPoint p2 = ccp( r.origin.x+r.size.width/2,r.origin.y+r.size.height/2);// .getPosition();
	float x = p.x - p2.x;
	float y = p.y - p2.y;
	double jiao = atan2(y, x);
	//Log.d("tag",String.valueOf(jiao));
	/*if(fabs(x)<r.size.width/5 && fabs(y) <r.size.width/5)
	{
		this->tpressed_UP = false;this->control->jup->setVisible(false);
		this->tpressed_DOWN = false;this->control->jdown->setVisible(false);
		this->tpressed_LEFT = false;this->control->jleft->setVisible(false);
		this->tpressed_RIGHT = false;this->control->jright->setVisible(false);
		return ;
	}*/
	if (jiao>=-PI/4 && jiao<PI/4)
	{
		//CCLog("right");
		this->tpressed_UP = false;//this->control->jup->setVisible(false);
		this->tpressed_DOWN = false;//this->control->jdown->setVisible(false);
		this->tpressed_LEFT = false;//this->control->jleft->setVisible(false);
		this->tpressed_RIGHT = true;//this->control->jright->setVisible(true);
		return ;
	}
	else if(jiao>=PI/4 && jiao<PI*3/4)
	{
		this->tpressed_UP = true;//this->control->jup->setVisible(true);
		this->tpressed_DOWN = false;//this->control->jdown->setVisible(false);
		this->tpressed_LEFT = false;//this->control->jleft->setVisible(false);
		this->tpressed_RIGHT = false;//this->control->jright->setVisible(false);
		return ;
	}
	else if(jiao>=-PI*3/4 && jiao<-PI/4)
	{
		this->tpressed_UP = false;//this->control->jup->setVisible(false);
		this->tpressed_DOWN = true;//this->control->jdown->setVisible(true);
		this->tpressed_LEFT = false;//this->control->jleft->setVisible(false);
		this->tpressed_RIGHT = false;//this->control->jright->setVisible(false);
		return ;
	}
	else
	{
		//CCLog("left");
		this->tpressed_UP = false;//this->control->jup->setVisible(false);
		this->tpressed_DOWN = false;//this->control->jdown->setVisible(false);
		this->tpressed_LEFT = true;//this->control->jleft->setVisible(true);
		this->tpressed_RIGHT = false;//this->control->jright->setVisible(false);
		return ;
	}
	//return 3;
}

void MyMain::ccTouchesMoved(CCSet *pTouches, CCEvent *pEvent) 
{

	CCSetIterator it;
	CCTouch* touch;

	for( it = pTouches->begin(); it != pTouches->end(); it++)
	{
		touch = (CCTouch*)(*it);

		if(!touch)
			break;

		CCPoint location = touch->getLocationInView();

		location = CCDirector::sharedDirector()->convertToGL(location);
		//CCLog("%d",touch->id());

		//addNewSpriteWithCoords( location );
		this->onMouseMove(touch->getID(),location);
		//this->cannon->setRotation(-90);

	}
}
void MyMain::ccTouchesEnded(CCSet *pTouches, CCEvent *pEvent)
{
	CCSetIterator it;
	CCTouch* touch;

	for( it = pTouches->begin(); it != pTouches->end(); it++)
	{
		touch = (CCTouch*)(*it);

		if(!touch)
			break;

		CCPoint location = touch->getLocationInView();

		location = CCDirector::sharedDirector()->convertToGL(location);
		CCLog("%d",touch->getID());

		//addNewSpriteWithCoords( location );
		this->onMouseUp(touch->getID(),location);
		//this->cannon->setRotation(-90);

	}
} 
void MyMain::ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent) 
{

	CCSetIterator it;
	CCTouch* touch;

	for( it = pTouches->begin(); it != pTouches->end(); it++)
	{
		touch = (CCTouch*)(*it);

		if(!touch)
			break;

		CCPoint location = touch->getLocationInView();

		location = CCDirector::sharedDirector()->convertToGL(location);

		//addNewSpriteWithCoords( location );
		this->onMouseDown(touch->getID(),location);
		//this->cannon->setRotation(-90);

	}
}
void MyMain::onMouseUp(int pid,CCPoint p)
{
	if (pid==did ) {
		did=-1;
		this->tpressed_UP = false;//this->control->jup->setVisible(false);
		this->tpressed_DOWN = false;//this->control->jdown->setVisible(false);
		this->tpressed_LEFT = false;//this->control->jleft->setVisible(false);
		this->tpressed_RIGHT = false;//this->control->jright->setVisible(false);

	} else if (pid==sid ) {
		sid=-1;
		this->tpressed_SPACE = false;
		//control->shoot->gotoAndStop(1);
	}

}
void MyMain::onMouseMove(int pid,CCPoint p)
{
	if(control==NULL) return;
	CCRect r =  CCRectMake(control->getPosition().x
		+ control->direction->getPosition().x-control->direction->getContentSize().width/2,
		control->getPosition().y
		+ control->direction->getPosition().y-control->direction->getContentSize().height/2,
		control->direction->getContentSize().width,
		control->direction->getContentSize().height);
	CCRect rs =  CCRectMake(
		control->getPosition().x + control->shoot->getPosition().x-control->shoot->getContentSize().width/2,
		control->getPosition().y + control->shoot->getPosition().y-control->shoot->getContentSize().height/2,
		control->shoot->getContentSize().width,
		control->shoot->getContentSize().height);
	CCLog("did=%d,sid=%d",did,sid);
	if(pid==did)//old pointer
	{
		if (this->containp(r, p)) {//in move
			this->directp(r, p);
		} else {//move out
			did=-1;
			this->tpressed_RIGHT = false;//this->control->jup->setVisible(false);
			this->tpressed_DOWN =false;//this->control->jdown->setVisible(false);
			this->tpressed_UP = false;//this->control->jleft->setVisible(false);
			this->tpressed_LEFT = false;//this->control->jright->setVisible(false);
		}

	}
	else//new pointer
	{
		if (this->containp(r, p)) {// move in
			did=pid;
			this->directp(r, p);
		} else {//out move
		}
	}
	if(sid==pid)//old pointer
	{
		if (this->containp(rs, p)) {//in move
			this->tpressed_SPACE = true;
			//control->shoot->gotoAndStop(2);


		} else {//move out
			sid=-1;
			this->tpressed_SPACE = false;
			//control->shoot->gotoAndStop(1);
		}

	}
	else//new pointer
	{
		if (this->containp(rs, p)) {// move in
			sid=pid;
			this->tpressed_SPACE = true;
			//control->shoot->gotoAndStop(2);

		} else {//out move
		}
	}

}
void MyMain::onMouseDown(int pid,CCPoint p)
{

	if (control != NULL) {

		CCRect r =  CCRectMake(control->getPosition().x
			+ control->direction->getPosition().x-control->direction->getContentSize().width/2,
			control->getPosition().y
			+ control->direction->getPosition().y-control->direction->getContentSize().height/2,
			control->direction->getContentSize().width,
			control->direction->getContentSize().height);
		CCRect rs =  CCRectMake(
			control->getPosition().x + control->shoot->getPosition().x-control->shoot->getContentSize().width/2,
			control->getPosition().y + control->shoot->getPosition().y-control->shoot->getContentSize().height/2,
			control->shoot->getContentSize().width,
			control->shoot->getContentSize().height);
		if (this->containp(r, p)) {
			did=pid;
			this->directp(r, p);
		} else if (this->containp(rs, p)) {
			sid=pid;
			this->tpressed_SPACE = true;
			//control->shoot->gotoAndStop(2);
		}
	}

}

//boolccTouchesMoved(MotionEvent arg0) {
//// arg0.get
//return false;
//}
//
//bool ccTouchesEnded(MotionEvent arg0) {
//// if (pTouches.length <= 0)
//// return;
////
//// touch = pTouches[0];
//// location = touch.locationInView(touch.view());
//return true;
//}

MyMain::MyMain()
{
	//tankstage =NULL;
	did=-1;
	sid=-1;
	//this->control=NULL;
	this->tpressed_DOWN=false;
	this->tpressed_LEFT=false;
	this->tpressed_RIGHT=false;
	this->tpressed_SPACE=false;
	this->tpressed_UP=false;
	control=NULL;
	debugct=0;
	lastchongtu=CCRectMake(0,0,0,0);
	this->homewalls.push_back(23*100+11);//lineIndex* 100 + colIndex
	this->homewalls.push_back(23*100+12);
	this->homewalls.push_back(23*100+13);
	this->homewalls.push_back(23*100+14);
	this->homewalls.push_back(24*100+11);//lineIndex* 100 + colInde
	//this->homewalls.push_back(24*100+13);
	//this->homewalls.push_back(24*100+14);
	this->homewalls.push_back(24*100+14);
	this->homewalls.push_back(25*100+11);//lineIndex* 100 + colIndex
	//this->homewalls.push_back(25*100+13);
	//this->homewalls.push_back(25*100+14);
	this->homewalls.push_back(25*100+14);
    this->tankScore=NULL;
}
bool MyMain::init()
{
	if ( !CCLayer::init() )
	{
		return false;
	}

	//timeAdd = 120;
	timeTank = 0;

	timebullet = 0;

	offerX = 2; // 36;//34
	offerY = 700-73-416-16; // 17;

	timeOver = 0;

	global_instance = this;
	setTouchEnabled(true);//setIsTouchEnabled(true);
	setKeypadEnabled(true);
	
	
    this->m_target=CCRenderTexture::create(420,700);
	//m_target->setAnchorPoint(ccp(0,0));
	m_target->setPosition(ccp(210,350));
    m_target->clear(0.5,0.5,0.5,1);
    addChild(m_target);
    
    this->m_target2=CCRenderTexture::create(416,416);
	//m_target->setAnchorPoint(ccp(0,0));
	m_target2->setPosition(ccp(offerX+416/2,offerY+416/2));
    m_target2->clear(0,0,0,1);
    addChild(m_target2);
    

	CCTexture2D *texture = CCTextureCache::sharedTextureCache()->textureForKey("images.png");
	bullet =  CCSpriteBatchNode::createWithTexture(texture);
	grass = CCSpriteBatchNode::createWithTexture(texture);
	bonus = CCNode::create();

	map =  CCSpriteBatchNode::createWithTexture(texture);
	//controls = CCNode::create();
	//drawl=CCNode::create();
    CCSprite *t1 = CCSprite::createWithSpriteFrameName("battlecity.png");
	t1->setAnchorPoint( ccp(0, 0));
	t1->setPosition(ccp(5,700-73));
	addChild(t1);
	addChild(map);
	addChild(bullet);
	addChild(grass);
	addChild(bonus);
	//addChild(drawl);
	
	this->_lbstage = CCLabelBMFont::create("0","font1.fnt");// CCLabelTTF::labelWithString("0", "Arial", 14);
	_lbstage->setPosition(ccp(64,this->offerY+416+7));
	_lbstage->setColor(ccc3(255, 255, 255));
	addChild(_lbstage);
	_lbstage->setScale(0.7);
	//_lbstage->setScaleY(0.5);
	this->_lbleave = CCLabelBMFont::create("enemy 20","font1.fnt");// CCLabelTTF::labelWithString("0",
	_lbleave->setPosition(ccp(288+70,this->offerY+416+7));
	_lbleave->setColor(ccc3(255, 255, 255));
	addChild(_lbleave);
	_lbleave->setScale(0.7);
	//_lbleave->setScaleY(0.7);
	this->_lblife = CCLabelBMFont::create("life 5","font1.fnt");// CCLabelTTF::labelWithString("0",
	_lblife->setPosition(ccp(210,this->offerY+416+7));
	_lblife->setColor(ccc3(255, 255, 255));
	addChild(_lblife);
	//_lblife->setScaleX(1);
	_lblife->setScale(0.7);

	bonus->setPosition( ccp(offerX, offerY));
	map->setPosition( ccp(offerX, offerY));
	grass->setPosition( ccp(offerX, offerY));
	bullet->setPosition( ccp(offerX, offerY));
	if(true)
	{
		map_allInit();
		initLevel(map_level);
	}
	else
	{
        map_levelScore[0]=3;
        map_levelScore[1]=14;

        map_levelScore[2]=3;
        map_levelScore[3]=3;

		this->testScore();
        //Dialog* d=new Dialog();d->autorelease();
        //addChild(d);
	}
	control = new Control();control->autorelease();
	addChild(control);
	//this->scheduleUpdate();
	return true;
}

void MyMain::testScore() {
	tankScore = new TankScore();tankScore->autorelease();
	this->addChild(tankScore,1000);
}

void MyMain::update(float dt) {
	//if (!map_getplay()) return;
	if (this->addtank) {
		this->addTanks(dt);
	}

	{
		for (int i = 0; i < map_tankArr->count(); i++)
		{
			Tank *tank= (Tank *)map_tankArr->objectAtIndex(i);
			if(tank!=NULL)//mytank maybe null
				tank->update(dt);
		}
		CCArray *cs=global_instance->bullet->getChildren();
		if (cs!=NULL)
		{
			for (int p = 0; p < cs->count(); p++)
			{
				Bullet * bullet=(Bullet *)cs->objectAtIndex(p);
				bullet->update(dt);
			}
		}
		//timeBullet++;
		timebullet+=dt;


		if(global_myTank!=NULL)
		{
			if (tpressed_DOWN)
			{
				global_myTank->move(dt,2);
				// ma MySound.myMove.play();
				if (global_sound) {
					//todo SoundEngine.playEffect(Resource.s_buttonEffect);
					//CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("SoundMove.mp3",false);
				}

			}

			else if (tpressed_UP)
			{
				global_myTank->move(dt,1);
				if (global_sound) {
					//todo SoundEngine.playEffect(Resource.s_buttonEffect);
					//CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("SoundMove.mp3");
				}
			}

			else if (tpressed_LEFT)
				// Tools.isDown(65))
			{
				global_myTank->move(dt,3);
				if (global_sound) {
					//todo SoundEngine.playEffect(Resource.s_buttonEffect);
					//CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("SoundMove.mp3");
				}
			}

			else if (tpressed_RIGHT)
			{
				global_myTank->move(dt,4);
				if (global_sound) {
					//todo SoundEngine.playEffect(Resource.s_buttonEffect);
					//CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("SoundMove.mp3");
				}
			}

			if (tpressed_SPACE && timebullet>global_shotLevel) {
				timebullet=0;
				global_myTank->shot();
				if (global_sound) {
					//todo SoundEngine.playEffect(Resource.s_buttonEffect);
					CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("SoundShot.mp3",false);
				}
			}
		}
		/*if (global_myTank==NULL  && map_life > 0)
		{
		MyMain::initMyTank();// in Bullet.cpp
		}*/
	}
	//map_changeplay(false);
	

	if (global_tank1num == 0 && map_leave == 0) {
		timeOver++;
		if (timeOver == 150) {
			//map_changeplay(false);
			tankScore = new TankScore();tankScore->autorelease();
			this->addChild(tankScore,1000);
            this->unscheduleUpdate();
            map_isPlay=false;
		}
	}
	/*if (map_leave < 0)
	map_leave = 0;
	if (global_tank1num < 0)
	global_tank1num = 0;
	*/
	/*if (global_scoreout == true) {
		global_scoreout = false;
		nextLevel();
	}*/
}

void MyMain::replay() {
	clearAll();
	//map_initMap(map_level);
	map_allInit();

	initLevel(map_level);
}

void MyMain::addTanks(float dt) {

	if (global_tank1num < 4)
		timeTank+=dt;

	// var location : int = timeTank % timeAdd;

	if (timeTank>3  && map_leave != 0) {
		timeTank=0;
		//int n = timeTank / timeAdd - 1;
		//ma Random r = new Random();
		int at = (((float)rand()/(RAND_MAX+1.0))*3);// * 3;
		int t = (((float)rand()/(RAND_MAX+1.0))*3);// * 3;

		TankBegin* begin;
		//n = 0;
		begin = new TankBegin(map_location[at], 416-16, t+1);begin->autorelease();
		map->addChild(begin);

		map_leave--; // = 5;//--;

		this->showenemyNum();
		// tankNum.gotoAndStop((20 - map_leave + 1));
		if (map_leave % 5 == 1)
			setBonus();
	}


}

void MyMain::gameOver() {
	global_nowScore = map_score;
	mygameover = new GameOver(offerX+208, offerY);mygameover->autorelease();
	//map_changeplay(false);
    this->unscheduleUpdate();map_isPlay=false;
	global_run = false;
	map_over = true;
	map_life=NUMLIFE;
	//global_bonusdie= true;
	
	global_instance->addChild(mygameover,1000);
}

void MyMain::nextLevel() {
	clearAll();

	//global_bonusdie= true;

	map_level++;
	// ma MySharedObjectExample.setCur(map_level);

	if (map_level > LEVELS)
		map_level = 1;
	CCUserDefault::sharedUserDefault()->setIntegerForKey(ID_LEVEL,map_level);
	map_allInit();
	initLevel(map_level);
}

void MyMain::initMyTank() {

	global_myTank = new Tank0(128+16+8 , 0+16);global_myTank->autorelease();
	global_instance->map->addChild(global_myTank);
	map_tankArr->addObject(global_myTank);
	//map_changeplay(true);
}
void MyMain::destroyme()
{

	global_myTank->destory();
	if(map_life>0)
	{
		global_shotLevel =INIT_SHOTLEVEL;
		global_instance->initMyTank();
	}
    else
    {
        gameOver();
    }
}
// static void setBoard()
// {
// MochiScores.showLeaderboard({boardID: MyMain.boardID, score:
// MyMain.nowScore});
// }

void MyMain::initLevel(int level) {
	alreadygrid=false;
	SetStage *setStage = new SetStage(level);setStage->autorelease();
	addChild(setStage,1000);
	//map_changeplay(true);
	//ma MapLevel.initMap(level);
	map_drawMap(map, grass);
	//tankstage->draw(level);
	//CCSize sz=getDeviceResolutionX();
	sprintf(a,"stage %d",level);
	_lbstage->setString(a);
	this->showenemyNum();showLife();
	timeTank = 1;
	timeOver = 1;

	global_run = true;

	map_levelScore[0] = 0;// = [0, 0, 0];
	map_levelScore[1] = 0;
	map_levelScore[2] = 0;
    map_levelScore[3] = 0;
	this->addtank = true;
	//global_isBomb = true;

	//global_bonusdie= false;
}

void MyMain::clearAll() {
	//clearTank();
	global_myTank=NULL;
	map->removeAllChildrenWithCleanup(true);
	grass->removeAllChildrenWithCleanup(true);
	bullet->removeAllChildrenWithCleanup(true);
	bonus->removeAllChildrenWithCleanup(true);
	// stage.removeEventListener(Event.ENTER_FRAME, addTanks);
	this->addtank = false;
}

void MyMain::setBonus() {
	Bonus* bon = new Bonus();bon->autorelease();
	bonus->addChild(bon);
}

void MyMain::map_drawMap(cocos2d::CCNode* mc, cocos2d::CCNode* grasss)
{
	//int rows = arr.length;
	//bubCont=new Sprite();
	//addChild(bubCont);
	//std::vector<int> test;
	//test.push_back(1);
	//test.push_back(2);
	//return ;
	unsigned char * pchar=NULL;
	pchar=levels.levelStrArr[map_level-1];
	int i=0;
	int lineIndex=0;
	int colIndex=0;
	while(true)
	{
		unsigned char c=*(pchar+i);
		if(*(pchar+i)=='\n' && *(pchar+i+1)=='\n' && *(pchar+i+2)=='\n')
		{
			break;
		}
		if(*(pchar+i)=='\n')
		{
			lineIndex++;
			colIndex=0;
		}
		if (c>='0' && c<='9')
		{
			int v=c-'0';map_map1[lineIndex][colIndex]=v;
			switch (v)
			{
			case 0:
				break;
			case 1:
				{
					Wall* wall = new Wall();wall->autorelease();

					int x = colIndex * 16;
					int y =(25- lineIndex) * 16;
					wall->setPosition(ccp(x, y));
					map_wallArr->setObject(wall,lineIndex* 100 + colIndex);
					mc->addChild(wall);
					break;
				}
			case 2:
				{
					Grid* grid = new Grid();grid->autorelease();

					int x =colIndex* 16;
					int y = (25-lineIndex) * 16;
					grid->setPosition(ccp(x, y));
					mc->addChild(grid);
					break;
				}
			case 3:
				{
					Grass* grass = new Grass();grass->autorelease();

					int x = colIndex* 16;
					int y = (25-lineIndex) * 16;
					grass->setPosition(ccp(x, y));
					grasss->addChild(grass);
					break;
				}
			case 4:
				{
					Water* water = new Water();water->autorelease();

					int x = colIndex* 16;
					int y = (25-lineIndex) * 16;
					water->setPosition(ccp(x, y));
					mc->addChild(water);
					break;
				}
			case 5:
				{
					Ice *ice = new Ice();ice->autorelease();

					int x = colIndex * 16;
					int y = (25-lineIndex) * 16;
					ice->setPosition(ccp(x, y));
					mc->addChild(ice);
					break;
				}
			case 9:
				{
					Home *home = new Home();home->autorelease();

					int x = colIndex * 16;
					int y = (25-lineIndex-1) * 16;
					home->setPosition(ccp(x, y));
					mc->addChild(home);
					break;
				}
			default:
				break;
			}
			colIndex++;
		}
		pchar++;//next char
	}

}
void MyMain::keyBackClicked(){

	CCScene *scene = CCScene::create();
	scene->addChild(SysMenu::create());
	CCDirector::sharedDirector()->replaceScene(scene);
}
bool MyMain::eqRect(CCRect r1, CCRect r2)
{
	if( eqPoint(r1.origin,r2.origin) && eqSize(r1.size,r2.size))
		return true;
	else
		return false;
}
bool MyMain::eqFloat(float a,float b)
{
	if(fabs(a-b)<5)
		return true;
	else
		return false;
}
bool MyMain::eqPoint(CCPoint a,CCPoint  b)
{
	if(eqFloat(a.x,b.x) && eqFloat(a.y,b.y))//, <#float b#>), <#float b#>))
		return true;
	else
		return false;
}
bool MyMain::eqSize(CCSize a,CCSize  b)
{
	if(eqFloat(a.width,b.width) && eqFloat(a.height,b.height))//, <#float b#>), <#float b#>))

		return true;
	else
		return false;
}
void MyMain::showChongtu(CCRect r,int r1,int r2,int c1,int c2,int row,int col)
{
	return;
	if (eqRect(r,lastchongtu))
	{
		return;
	}
	lastchongtu=r;
	debugct++;
	CCRenderTexture*cur_target;
	//if(debugct%2==0)
	//{
	//    cur_target=m_target;
	//}
	//else
	//{
	//    cur_target=m_target2;
	//}
	cur_target->clear(0,0,0,0);//, <#float g#>, <#float b#>, <#float a#>)
	ccColor4F c=ccc4FFromccc4B(ccc4(0,255,0,1));

	drawRect(cur_target,r.origin,ccp(r.origin.x+r.size.width,r.origin.y+r.size.height),c);
	CCPoint start =ccp(c1*16,r1*16);
	CCPoint end = ccp(c2*16+16,r2*16+16);

	drawRect(cur_target,start,end,c);
	ccColor4F color2=ccc4FFromccc4B(ccc4(255,255,255,1));
	//drawRect(ccp(col*16,row*16),ccp(col*16+16,row*16+16),color2);
	drawCircle(cur_target,ccp(col*16+8,row*16+8),color2);//, <#ccColor4F color#>)

}
void MyMain::drawCircle(CCRenderTexture*m_target,CCPoint p1,ccColor4F color)
{

	m_target->begin();
	::glLineWidth(2);
	//::glColor4f(color.r,color.g,color.b,color.a);
	::ccDrawCircle(p1, 8,360,8,true);
	//::ccDrawCircle(<#const cocos2d::CCPoint &center#>, <#float radius#>, <#float angle#>, <#int segments#>, <#bool drawLineToCenter#>)
	m_target->end();
}
void MyMain::showenemyNum()
{
	sprintf(a,"enemy %d",map_leave+global_tank1num);
	_lbleave->setString(a);
}
void MyMain::showLife()
{
	sprintf(a,"life %d",map_life);
	_lblife->setString(a);
}
void MyMain:: repair()
{
	//for (int i = 0; i < c.size(); i++) {//todo 
	//	if (c[i] == b) {

	//		find=true;
	//		at=i;
	//		break;
	//	}
	//}
	//if(find)
	//{
	//	c.erase(c.begin()+at);
	//}
	if (alreadygrid) return;
	bool already= false;
	//bool isWall = true;
	//if(homewalls.size()<1) return;//already grid
	int ctwall=0;
	for (int i = 0; i < homewalls.size(); i++)
	{
		int k=homewalls[i];
		Wall * w=(Wall *)map_wallArr->objectForKey(k);
		if ( w != NULL)
		{
			if(w->life<1)
			{
				w->life=1;
				already=true;
			}
			ctwall++;
		}
		else//repair
		{
			//lineIndex* 100 + colIndex
			int lineIndex=k/100;
			int colIndex=k % 100;
			already=true;
			map_map1[lineIndex][colIndex]=1;
			Wall* wall = new Wall();wall->autorelease();
			int x = colIndex * 16;
			int y =(25- lineIndex) * 16;
			wall->setPosition(ccp(x, y));
			map_wallArr->setObject(wall,lineIndex* 100 + colIndex);
			map->addChild(wall);
		}
	}//end for
	if (already) return;//already repair wall 
	walltogrid();
}
void MyMain::walltogrid()
{
	alreadygrid=true;
	for (int i = 0; i < homewalls.size(); i++)
	{
		int k=homewalls[i];
		Wall * w=(Wall *)map_wallArr->objectForKey(k);
		if ( w != NULL)
		{
			int lineIndex=k/100;
			int colIndex=k % 100;
			map_map1[lineIndex][colIndex]=2;
			Grid* grid = new Grid();grid->autorelease();
			int x = colIndex * 16;
			int y =(25- lineIndex) * 16;
			grid->setPosition(ccp(x, y));
			map->addChild(grid);

			map_wallArr->removeObjectForKey(k);
			map->removeChild(w,true);
		}
		else//repair
		{
			//lineIndex* 100 + colIndex
			//shoud not occur
			;
		}
	}//end for
}
